package com.situ.util;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBUtil {

    public static Connection getConnection() {
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.创建连接
            return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/rbac", "root", "12345678");
        } catch (Exception e) {
            System.out.println("获取数据库连接失败");
        }
        return null;
    }

    public static Long executeCount(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            //3.准备sql
            ps = conn.prepareStatement(sql);
            if (params != null && params.length > 0) {
                for (int i = 1; i <= params.length; i++) {
                    ps.setObject(i, params[i - 1]);
                }
            }
            //4.执行查询
            rs = ps.executeQuery();
            rs.next();
            return rs.getLong(1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(rs, ps, conn);
        }
        return 0L;
    }

    public static <T> List<T> executeDQL(String sql, Class<T> c, Object... params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<T> list = new ArrayList<>();
        try {
            conn = getConnection();
            //3.准备sql
            ps = conn.prepareStatement(sql);
            if (params != null && params.length > 0) {
                for (int i = 1; i <= params.length; i++) {
                    ps.setObject(i, params[i - 1]);
                }
            }
            //4.执行查询
            rs = ps.executeQuery();
            //获取结果集中的元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            //5.解析结果集
            while (rs.next()) {
                T t = c.newInstance();
                //循环每一列
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    String columnName = rsmd.getColumnLabel(i);
                    Object value = rs.getObject(columnName);
                    try {
                        Field field = c.getDeclaredField(columnName);
                        field.setAccessible(true);
                        if (field.getType().equals(Boolean.class)) {//如果类中是布尔值，只放value是否为空
                            field.set(t, value != null);
                        } else {
                            field.set(t, value);
                        }
                    } catch (Exception e) {
                        System.out.println(columnName + "：这一列没找到");
                    }
                }
                list.add(t);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            close(rs, ps, conn);
        }
        return list;
    }


    public static int executeDML(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConnection();
            //3.准备sql
            ps = conn.prepareStatement(sql);
            if (params != null && params.length > 0) {
                for (int i = 1; i <= params.length; i++) {
                    ps.setObject(i, params[i - 1]);
                }
            }
            //4.执行sql
            int result = ps.executeUpdate();
            return result;
        } catch (Exception e) {
            System.out.println("执行insert异常了");
        } finally {
            close(null, ps, conn);
        }
        return 0;
    }

    private static void close(ResultSet rs, PreparedStatement ps, Connection conn) {
        try {
            rs.close();
        } catch (Exception e) {

        }
        try {
            ps.close();
        } catch (Exception e) {

        }
        try {
            conn.close();
        } catch (Exception e) {

        }
    }

}
